<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
/*
	广告值结算
*/
defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$return['params']=array();
	if(!empty($_RQ['year'])){
		$params['year']=$_RQ['year'];
		$return['params']['year']=$_RQ['year'];
	}
	if(!empty($_RQ['month'])){
		$params['month']=$_RQ['month'];
		$return['params']['month']=$_RQ['month'];
	}
	//时间范围
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$return['params']['createtime']=$time_range;
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
	}
	$list=pdo_getslice('jy_jindoufee_yuelog',$params,array($pindex,$psize),$total,array(),'','id desc');
	foreach($list as &$item){
		$item['member']=cfc('core')->member_show($item['mid']);
		$item['agent']=cfc('core')->member_show($item['agent_id']);
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['htmls']=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'广告值释放记录'),
			array('op'=>'up','title'=>'操作广告值释放'),
		),
		'list'=>array('data'=>'list','edit'=>false,'list'=>array(
			array('name'=>'level_id','title'=>'分销商等级','type'=>'select','options'=>'levels'),
			array('name'=>'year','title'=>'年份','type'=>'string'),
			array('name'=>'month','title'=>'月份','type'=>'string'),
			array('name'=>'week','title'=>'周','type'=>'string'),
			array('name'=>'day','title'=>'天','type'=>'string'),
			array('name'=>'num','title'=>'总人数','type'=>'string'),
			array('name'=>'allred','title'=>'总金额','type'=>'string'),
			array('name'=>'allfee','title'=>'总业绩','type'=>'string'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
		)),
		'search'=>array(
			array('name'=>'year','width'=>15,'title'=>'年份','type'=>'string'),
			array('type'=>'month','width'=>15,'title'=>'月份','type'=>'string'),
		),
	);
	$return['levels']=pdo_getall('jy_jindoufee_level',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	exi($return);
}
if($_RQ['op']=='up'){
	$setting=$this->setting;
	$year=intval(date('Y',time()));
	$return['year']=array('0'=>array('id'=>$year-9,'title'=>$year-9),'1'=>array('id'=>$year-8,'title'=>$year-8),'2'=>array('id'=>$year-7,'title'=>$year-7),'3'=>array('id'=>$year-6,'title'=>$year-6),'4'=>array('id'=>$year-5,'title'=>$year-5),'5'=>array('id'=>$year-4,'title'=>$year-4),'6'=>array('id'=>$year-3,'title'=>$year-3),'7'=>array('id'=>$year-2,'title'=>$year-2),'8'=>array('id'=>$year-1,'title'=>$year-1),'9'=>array('id'=>$year,'title'=>$year));
	$return['month']=array('0'=>array('id'=>1,'title'=>1),'1'=>array('id'=>2,'title'=>2),'2'=>array('id'=>3,'title'=>3),'3'=>array('id'=>4,'title'=>4),'4'=>array('id'=>5,'title'=>5),'5'=>array('id'=>6,'title'=>6),'6'=>array('id'=>7,'title'=>7),'7'=>array('id'=>8,'title'=>8),'8'=>array('id'=>9,'title'=>9),'9'=>array('id'=>10,'title'=>10),'10'=>array('id'=>11,'title'=>11),'11'=>array('id'=>12,'title'=>12));
	$return['week']=array(
		array('id'=>1,'title'=>'第一周'),
		array('id'=>2,'title'=>'第二周'),
		array('id'=>3,'title'=>'第三周'),
		array('id'=>4,'title'=>'第四周')
	);
	if($setting['zhouqi']==2){
		$htmls=array(
			'type'=>'edit',
			'post'=>'post',
			'tabs'=>array(
				array('op'=>'list','title'=>'结算单'),
			),
			'edit'=>array(
				array('name'=>'year','title'=>'选择年份','type'=>'select','options'=>'year'),
				array('name'=>'month','title'=>'选择月份','type'=>'select','options'=>'month'),
				array('name'=>'week','title'=>'选择周数','type'=>'select','options'=>'week'),
			)
		);
	}elseif($setting['zhouqi']==1){
		$htmls=array(
			'type'=>'edit',
			'post'=>'post',
			'tabs'=>array(
				array('op'=>'list','title'=>'结算单'),
			),
			'edit'=>array(
				array('name'=>'year','title'=>'选择年份','type'=>'select','options'=>'year'),
				array('name'=>'month','title'=>'选择月份','type'=>'select','options'=>'month'),
			)
		);
	}else{
		$htmls=array(
			'type'=>'edit',
			'post'=>'post',
			'tabs'=>array(
				array('op'=>'list','title'=>'结算单'),
			),
			'edit'=>array(
				array('name'=>'day','title'=>'选择日期','type'=>'time','time_type'=>'day'),
			)
		);
	}
	$return['item']=array();
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['day'] && !$postdata['year']){
		exi('请选择年份','error');
	}
	if(!$postdata['day'] && !$postdata['month']){
		exi('请选择月份','error');
	} 
	if($postdata['month']<10){
		$postdata['month']='0'.$postdata['month'];
	}
	$params=array('uniacid'=>$_SESSION['uniacid'],'year'=>$postdata['year'],'month'=>$postdata['month']);
	if($postdata['day']){
		$params['day']=$postdata['day'];
		$firstday = strtotime($postdata['day']);
		$lastday = $firstday+24*3600;
	}elseif(!$postdata['week']){
		$firstday = strtotime($postdata['year'] .$postdata['month'] . "01000000");
		$firstdaystr = date("Y-m-01", $firstday);
		$lastday = strtotime(date('Y-m-d 23:59:59', strtotime("$firstdaystr +1 month -1 day")));
	}else{
		if($postdata['week']<4){
			$day=($postdata['week']-1)*7+1;
			if($day<10){
				$day='0'.$day;
			}
			$firstday = strtotime($postdata['year'] .$postdata['month'] .$day. "000000");
			$firstdaystr = date("Y-m-d", $firstday);
			$lastday = strtotime(date('Y-m-d 23:59:59', strtotime("$firstdaystr  +6 day")));
		}else{
			$firstday = strtotime($postdata['year'] .$postdata['month'] . "22000000");
			$firstdaystr = date("Y-m-01", $firstday);
			$lastday = strtotime(date('Y-m-d 23:59:59', strtotime("$firstdaystr +1 month -1 day")));
		}
		$params['week']=$postdata['week'];
	}
	if($lastday>time()){
		exi('当天结算周期还未结束！不能提交结算！','error');
	}
	$isadd=pdo_get('jy_jindoufee_yuelog',$params);
	if($isadd['id']){
		$date=$postdata['year'].'-'.$postdata['month'];
		if($postdata['week']){
			$date .='第'.$postdata['week'].'周';
		}
		if($postdata['day']){
			$date=$postdata['day'];
		}
		exi($date.'已提交结算，不可重复提交！','error');
	}
	//$params=array('uniacid'=>$_SESSION['uniacid'],'is_agent'=>1,'ggfee >'=>0,'agenttime <'=>$lastday);
	//$level_ids=pdo_getall('jy_jindoufee_level',array('uniacid'=>$_SESSION['uniacid'],'sfgg_num <>'=>''),'id','id');
	//$params['level_id']=$level_ids?array_keys($level_ids):-1;
	//$partnercount=pdo_count('jy_jindoufee_member',$params);
	$partnercount=pdo_count('jy_jindoufee_member',array('is_agent'=>1,'ggfee >'=>0,'uniacid'=>$_SESSION['uniacid']));
	$setting=$this->setting;
	$ggprice=pdo_getsum('jy_jindoufee_order',array('uniacid'=>$_SESSION['uniacid'],'ispay'=>1,'createtime between'=>array($firstday,$lastday)),'ggprice');
	$orderfee=pdo_getsum('jy_jindoufee_order',array('uniacid'=>$_SESSION['uniacid'],'ispay'=>1,'createtime between'=>array($firstday,$lastday)),'price');
	$money=0;
	$bonusmoney=0;
	if(!$partnercount){
		exi('没有可释放的分销商','error');
	}
	if(!$ggprice && !$orderfee){
		exi('该时间段没有营业额！','error');
	}
	$item['year']=$postdata['year'];
	$item['month']=$postdata['month'];
	$item['day']=$postdata['day'];
	$item['starttime']=$firstday;
	$item['endtime']=$lastday;
	$item['week']=$postdata['week'];
	$item['num']=$partnercount;
	$item['orderfee']=$orderfee;
	$item['ggprice']=$ggprice;
	$this->session('item',$item);
	exi('','','step1');
}
if($_RQ['op']=='step1'){
	$return['step']=1;
	$item=$this->session('item');
	$item['week2']=$item['week'];
	$item['year2']=$item['year'];
	$item['isday']=$item['day']?1:0;
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'do',
		'edit'=>array(
			array('name'=>'year2','title'=>'年份','type'=>'string','read'=>true,'show'=>array('isday'=>0)),
			array('name'=>'month','title'=>'月份','type'=>'string','read'=>true,'show'=>array('isday'=>0)),
			array('name'=>'week2','title'=>'周','type'=>'string','read'=>true,'show'=>array('week2 >'=>0,'isday'=>0)),
			array('name'=>'day','title'=>'日期','type'=>'string','read'=>true,'show'=>array('isday'=>1)),
			array('name'=>'orderfee','title'=>'运营额','type'=>'string','read'=>true),
			//array('name'=>'ggprice','title'=>'商品独立广告释放额','type'=>'string','read'=>true),
			array('name'=>'num','title'=>'分销商数量','type'=>'string','read'=>true),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='do'){
	$postdata=get_postdata();
	$data=$postdata;
	$data['year']=$data['year2'];
	$data['month']=$data['month'];
	$data['week']=$data['week2'];
	$data['day']=$data['day'];
	if($data['day']){
		$data['year']=date('Y',$data['starttime']);
		$data['month']=date('m',$data['starttime']);
	}
	$this->model('model')->upyuelog($data);
	exi('发放成功','','list');
}
if($_RQ['op'] == 'del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_jindoufee_yuelog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_jindoufee_yuelog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}